package top.mcwebsite.playandroid.business.me.impl.coin.ui

import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.layout.wrapContentWidth
import androidx.compose.foundation.lazy.LazyColumn
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.LocalContentColor
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import androidx.paging.LoadState
import androidx.paging.compose.LazyPagingItems
import top.mcwebsite.playandroid.business.me.impl.model.CoinFetchItemModel
import top.mcwebsite.playandroid.common.uiWidget.skeleton

@Composable
fun CoinContent(
    lazyPagingItems: LazyPagingItems<CoinFetchItemModel>,
) {
    if (lazyPagingItems.itemCount == 0 && lazyPagingItems.loadState.append == LoadState.NotLoading(true)) {
        if (lazyPagingItems.itemCount == 0) {
            Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {
                Text(text = "空空如也")
            }
        }
    }
    LazyColumn {
        if (lazyPagingItems.loadState.refresh == LoadState.Loading) {
            item {
                Column(
                    modifier = Modifier.fillMaxHeight()
                ) {
                    repeat(9) {
                        CoinFetchItemSkeleton()
                    }
                }
            }
        }

        items(count = lazyPagingItems.itemCount) { index ->
            lazyPagingItems[index]?.let { coinFetchItem ->
                CoinFetchItem(coinFetchItemModel = coinFetchItem)
            }
        }

        if (lazyPagingItems.loadState.append == LoadState.Loading) {
            item {
                CircularProgressIndicator(
                    modifier = Modifier
                        .fillMaxWidth()
                        .wrapContentWidth(Alignment.CenterHorizontally)
                )
            }
        }
    }
}

@Composable
fun CoinFetchItemSkeleton() {
    Row(
        verticalAlignment = Alignment.CenterVertically,
        modifier = Modifier.padding(horizontal = 12.dp, vertical = 10.dp),
    ) {
        Column(
            modifier = Modifier.weight(1f)
        ) {
            Text(text = "签到", modifier = Modifier
                .padding(vertical = 1.dp)
                .skeleton(true))
            Text(
                text = "",
                style = MaterialTheme.typography.labelMedium,
                color = LocalContentColor.current.copy(alpha = 0.7f),
                modifier = Modifier
                    .width(200.dp)
                    .padding(vertical = 1.dp)
                    .skeleton(true)
            )
        }
        Text(text = "", color = MaterialTheme.colorScheme.primary, modifier = Modifier
            .width(20.dp)
            .skeleton(true))
    }
}

@Composable
fun CoinFetchItem(coinFetchItemModel: CoinFetchItemModel) {
    Row(
        verticalAlignment = Alignment.CenterVertically,
        modifier = Modifier.padding(horizontal = 12.dp, vertical = 10.dp),
    ) {
        Column(
            modifier = Modifier.weight(1f)
        ) {
            Text(text = coinFetchItemModel.reason)
            Text(
                text = coinFetchItemModel.desc,
                style = MaterialTheme.typography.labelMedium,
                color = LocalContentColor.current.copy(alpha = 0.7f)
            )
        }
        Text(text = "+" + coinFetchItemModel.coinCount.toString(), color = MaterialTheme.colorScheme.primary)
    }
}
